<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="联系人" prop="userName">
        <el-input
          v-model="queryParams.serviceMatters"
          placeholder="请输入服务事项"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-tabs v-model="status" type="card" @tab-click="handleClick">
      <el-tab-pane label="待支付" name="1"></el-tab-pane>
      <el-tab-pane label="已支付" name="2"></el-tab-pane>
      <el-tab-pane label="已取消" name="3"></el-tab-pane>
    </el-tabs>
    <el-table v-loading="loading" :data="platformServiceOrderList">
      <el-table-column :formatter="defaultTableColumnDate" label="序号" align="center" type="index" />
      <el-table-column :formatter="defaultTableColumnDate" label="服务事项" align="center" prop="serviceMatters" />
      <el-table-column :formatter="defaultTableColumnDate" label="联系人" align="center" prop="userName" />
      <el-table-column :formatter="defaultTableColumnDate" label="手机号码" align="center" prop="phone" />
      <el-table-column :formatter="defaultTableColumnDate" label="提交时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :formatter="defaultTableColumnDate" label="服务费用" align="center" prop="serviceFee" />

      <el-table-column :formatter="defaultTableColumnDate" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:platformServiceOrder:edit']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog destroy-on-close :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" disabled :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务事项" prop="serviceMatters">
          <el-input v-model="form.serviceMatters" type="textarea" placeholder="-" />
        </el-form-item>
        <el-form-item label="提交日期" prop="payTime">
          <el-date-picker clearable
            v-model="form.createTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="-">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime" v-if="form.status == '2'">
          <el-date-picker clearable
            v-model="form.payTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="-">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="联系人" prop="userName">
          <el-input v-model="form.userName" placeholder="-" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="-" />
        </el-form-item>
        <el-form-item label="服务费用" prop="serviceFee">
          <el-input v-model="form.serviceFee" type="textarea" placeholder="-" />
        </el-form-item>
        <el-form-item label="服务说明" prop="serviceDescription">
          <el-input v-model="form.serviceDescription" type="textarea" placeholder="-"  />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="form.status == '1'" @click="submitForm">立即支付</el-button>
        <el-button @click="cancelOrder" v-if="form.status == '1'">取消订单</el-button>
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>

    <el-dialog destroy-on-close title="选择支付方式" :visible.sync="typeopen" width="800px" append-to-body>
      <div class="pay-type" v-if="!code">
        <img :src="wx" alt="" @click="getCode('1')">
        <img :src="zfb" alt="" @click="getCode('2')">
      </div>
      <div class="pay-type" v-if="code">
        <img :src="code" alt="">
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="typeopen = false;code=''">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import qrcode from 'qrcode'
import { listPlatformServiceOrder, getPlatformServiceOrder, payOrder, cancelOrderFn } from "@/api/system/platformServiceOrder";
import wx from '../../../assets/images/wx.png'
import zfb from '../../../assets/images/zfb.png'

let interval = null

export default {
  name: "PlatformServiceOrder",
  data() {
    return {
      wx,
      zfb,
      code: '',
      status: '1',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 平台服务订单表格数据
      platformServiceOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      typeopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        serviceMatters: null,
        serviceFee: null,
        enterprise: null,
        serviceDescription: null,
        userName: null,
        phone: null,
        status: null,
        payUser: null,
        payTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleClick(tab, event) {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getCode(payType) {
      this.$modal.loading();
      payOrder({ payType, id: this.form.id }).then(res => {
        this.$modal.closeLoading();
        if(payType=='2') {
          const div = document.createElement('divform');
          div.innerHTML = res.msg;
          document.body.appendChild(div);  
          // document.forms['punchout_form'].setAttribute('target', '_blank')
          document.forms['punchout_form'].submit()
        } else {
          qrcode.toDataURL(res.msg, {width: 200, height: 200}, (err, url) => {
            if (err) {
              this.$modal.msg('微信支付码生成失败')
            } else {
              this.code = url
            }
          });
        }
        this.getOrderStatus()
      })
    },
    getOrderStatus() {
      let time = 0
      const that = this
      interval = setInterval(async () => {
        const res = await getPlatformServiceOrder(that.form.id)
        if(res.code == 200) {
          const data = res.data
          if(data.status == '2') {
            that.$modal.msgSuccess('支付成功')
            that.cancel()
          } else {
            time = time + 3
            if(time >= 5 * 60) {
              that.$modal.msgError('支付超时')
              that.cancel()
            }
          }
        }
      }, 3000);
    },
    /** 查询平台服务订单列表 */
    getList() {
      this.loading = true;
      listPlatformServiceOrder({...this.queryParams, status: this.status}).then(response => {
        this.platformServiceOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    cancelOrder() {
      const id = this.form.id
      this.$modal.confirm('是否确认取消？').then(function() {
        return cancelOrderFn({id});
      }).then(() => {
        this.cancel()
        this.$modal.msgSuccess("取消成功");
      }).catch(() => {});
    },
    // 取消按钮
    cancel() {
      this.code = ''
      this.open = false;
      this.typeopen = false
      this.reset();
      clearInterval(interval)
      this.getList();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        serviceMatters: null,
        serviceFee: null,
        enterprise: null,
        serviceDescription: null,
        userName: null,
        phone: null,
        status: null,
        payUser: null,
        payTime: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPlatformServiceOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "平台服务订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.typeopen = true
    },
  }
};
</script>

<style lang="scss" scoped>
.pay-type {
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 36px;
  img {
    cursor: pointer;
    width: 200px;
    height: auto;
  }
}
</style>
